#include "wave_mdle.h"
#include "ui_wave_mdle.h"

wave_mdle::wave_mdle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::wave_mdle)
{
    ui->setupUi(this);

    for(int i =0; i < numb_monitor_graph; i++)
    {
        //add graph
        ui->wave_show->addGraph();
        //sete pen
        if(i % 4 == 0)
        {
           ui->wave_show->graph(i)->setPen(QPen(Qt::blue));
        }
        else if(i % 4 == 1)
        {
           ui->wave_show->graph(i)->setPen(QPen(Qt::green));
        }
        else if(i %4 == 2)
        {
            ui->wave_show->graph(i)->setPen(QPen(Qt::yellow));
        }
        else
        {
            ui->wave_show->graph(i)->setPen(QPen(Qt::red));
        }
        //set color
        //ui->wave_show->graph(0)->setBrush(QColor(255,255,0));

        //set name
        ui->wave_show->graph(i)->setName("wave monitor "+QString::number(i));

    }
    //set x and y
    ui->wave_show->xAxis->setLabel("time");
    ui->wave_show->yAxis->setLabel("data");
    //set range
    ui->wave_show->xAxis->setRange(0,60);
    ui->wave_range->setValue(12); //default 2 ^ 12
    ui->update_range->setValue(10); //2 ^ 10 max x
    ui->update_state->setMaximum(1 << 10); //state max value
    ui->wave_show->yAxis->setRange(0,5000);
    //zoom
    ui->wave_show->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));//
    ui->wave_show->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));//
    //ui->wave_show->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
    //move
    ui->wave_show->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    // --------------------------------------------
    // add vect init


}

wave_mdle::~wave_mdle()
{
    delete ui;
}

void wave_mdle::on_btn_demo_clicked()
{
    for(int i = 0; i < 100;i++)
    {
#ifdef DEMO1
        //gen dat
        dat_x.append(dat_x.count());
        dat_y.append(qrand()%4096);
        //set data
        ui->wave_show->graph(0)->setData(dat_x,dat_y);

        //wave point rewrite
        ui->wave_show->replot();
#else
        recv_data_monitor(qrand()%4096,1);
#endif
    }

    reset_plot_view();

}


void wave_mdle::on_wave_show_customContextMenuRequested(const QPoint &pos)
{
    qDebug() << pos;
    QMenu *menu = new QMenu(this);
    QAction * action0 = new QAction;
    QAction * action1 = new QAction;
    QAction * action2 = new QAction;
    action0->setText("zoom_x");
    action1->setText("reset_view");
    action2->setText("clear_data");

    connect(action0,SIGNAL(triggered(bool)),this,SLOT(zoom_plot_xview()));
    connect(action1,SIGNAL(triggered(bool)),this,SLOT(reset_plot_view()));
    connect(action2,SIGNAL(triggered(bool)),this,SLOT(clear_plot_data()));
    menu->addAction(action0);
    menu->addAction(action1);
    menu->addAction(action2);

    menu->exec(QCursor::pos());
}

void wave_mdle::reset_plot_view()
{
    //qDebug() << dat_x.length() << dat_x.size();
    ui->wave_show->xAxis->setRange(0,dat_x.length()); //
    int range = 1 << ui->wave_range->value();
    ui->wave_show->yAxis->setRange(0,range);
    ui->wave_show->replot();

}

void wave_mdle::clear_plot_data()
{
    ui->wave_show->clearGraphs();
    ui->wave_show->replot();
    for(int i =0; i < numb_monitor_graph; i++)
    {
        //add graph
        ui->wave_show->addGraph();
        //sete pen
        if(i % 4 == 0)
        {
           ui->wave_show->graph(i)->setPen(QPen(Qt::blue));
        }
        else if(i % 4 == 1)
        {
           ui->wave_show->graph(i)->setPen(QPen(Qt::green));
        }
        else if(i %4 == 2)
        {
            ui->wave_show->graph(i)->setPen(QPen(Qt::yellow));
        }
        else
        {
            ui->wave_show->graph(i)->setPen(QPen(Qt::red));
        }
        //set color
        //ui->wave_show->graph(0)->setBrush(QColor(255,255,0));

        //set name
        ui->wave_show->graph(i)->setName("wave monitor "+QString::number(i));
    } //expand N graph

}


void wave_mdle::zoom_plot_xview()
{
    //qDebug() << dat_x.length() << dat_x.size();
    ui->wave_show->xAxis->setRange(ui->wave_show->xAxis->range().lower/2,
                                   ui->wave_show->xAxis->range().upper/2); //
    ui->wave_show->replot();
}

void wave_mdle::recv_data_monitor(quint32 data,int line)
{
    if(line >= numb_monitor_graph)
    {
        return;
    }
    int numb_bef = ui->wave_show->graph(line)->dataCount();
    if(numb_bef > (1 << ui->update_range->value()))
    {
        clear_plot_data();
        ui->update_state->setValue(0);
        ui->update_state->setMaximum(1 << ui->update_range->value());
        numb_bef = 0;
    }
    //gen dat
    dat_x.clear();
    dat_y.clear();
    for(int i = 0;i < numb_bef; i ++)
    {
        dat_x.append(ui->wave_show->graph(line)->dataMainKey(i));
        dat_y.append(ui->wave_show->graph(line)->dataMainValue(i));
        //low speed andv multi wave mix
    }
    dat_x.append(numb_bef);
    dat_y.append(data);
    //set data
    ui->wave_show->graph(line)->setData(dat_x,dat_y,true); //update to all data
    //wave point rewrite
    ui->wave_show->replot();
    reset_plot_view();
    //update state
    ui->update_state->setValue(ui->update_state->value() + 1);
}

